<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
	xmlns:h="http://xmlns.jcp.org/jsf/html"
	xmlns:f="http://xmlns.jcp.org/jsf/core"
	xmlns:p="http://primefaces.org/ui"
	xmlns:sec="http://www.springframework.org/security/tags">

<ui:composition template="/template.xhtml">
	<ui:define name="title">
		SGMF :: Ordem de Serviço
	</ui:define>

	<ui:define name="content">
		<p:panel>
			<h:form id="buscarVeiculoForm">
				<p:fieldset legend="Buscar Veículo">
					<p:panelGrid columns="3" styleClass="borderless">
						<p:outputLabel value="Placa do Veículo" for="placaVeiculo" />
						<p:inputText id="placaVeiculo" value="#{ordemServicoBean.placa}" />
						<p:commandButton id="buscarVeiculo" update=":veiculoDialogForm" actionListener="#{ordemServicoBean.buscarVeiculo}" value="Buscar Veículo" icon="ui-icon-search" onclick="PF('veiculoDialog').show();" />
					</p:panelGrid>
				</p:fieldset>
			</h:form>
			<br />
			<h:form id="buscarPecaForm">
				<p:fieldset legend="Buscar Peça">
					<p:panelGrid columns="3" styleClass="borderless">
						<p:outputLabel value="Nome da Peça" for="nomePeca" />
						<p:inputText id="nomePeca" value="#{ordemServicoBean.nome}" />
						<p:commandButton id="buscarPeca" update=":pecaDialogForm" actionListener="#{ordemServicoBean.buscarPeca}" value="Buscar Peça" icon="ui-icon-search" onclick="PF('pecaDialog').show();" />
					</p:panelGrid>
				</p:fieldset>
			</h:form>
			<br />
			<h:form id="ordemServicoForm">
				<h:inputHidden id="ordemServicoId" value="#{ordemServicoBean.ordemServico.id}" rendered="#{ordemServicoBean.ordemServico.id != null}" />
				<p:fieldset legend="Ordem de Serviço">
					<p:panelGrid columns="3" styleClass="borderless">
						<p:outputLabel value="Data" for="data" />
						<h:outputText id="data" value="#{ordemServicoBean.data}">
							<f:convertDateTime dateStyle="medium" />
						</h:outputText>
						<h:outputText />
						<p:outputLabel value="Placa" for="placa" />
						<h:outputText id="placa" value="#{ordemServicoBean.veiculo.placa}" />
						<h:outputText />
						<p:outputLabel value="Chassi" for="chassi" />
						<h:outputText id="chassi" value="#{ordemServicoBean.veiculo.chassi}" />
						<h:outputText />
						<p:outputLabel value="Marca" for="marca" />
						<h:outputText id="marca" value="#{ordemServicoBean.veiculo.modelo.marca.nome}" />
						<h:outputText />
						<p:outputLabel value="Modelo" for="modelo" />
						<h:outputText id="modelo" value="#{ordemServicoBean.veiculo.modelo.nome}" />
						<h:outputText />
						<p:outputLabel value="Número" for="numero" />
						<p:inputText id="numero" label="Número" value="#{ordemServicoBean.ordemServico.numero}" required="true" />
						<p:message for="numero" />
						<p:outputLabel value="Status" for="status" />
							<p:selectOneMenu id="status" value="#{ordemServicoBean.ordemServico.statusOS}" required="true">
								<sec:authorize ifAnyGranted="ROLE_ADMIN, ROLE_ENCARREGADO, ROLE_CHEFE_MECANICA">
									<f:selectItems value="#{ordemServicoBean.statusOS}"	var="statusOS" itemValue="#{statusOS}" itemLabel="#{statusOS.statusOS}" />
								</sec:authorize>
								<sec:authorize hasRole="ROLE_APONTADOR">
									<f:selectItems value="#{ordemServicoBean.statusOS}"	var="statusOS" itemValue="#{statusOS}" itemLabel="#{statusOS.statusOS}" />
								</sec:authorize>
							</p:selectOneMenu>
							<p:message for="status" />
						<p:outputLabel value="Descrição do Problema" for="problema" />
						<p:inputTextarea id="problema" value="#{ordemServicoBean.ordemServico.problema}" rows="5" cols="30" maxlength="255" required="true" />
						<p:message for="problema" />
						<p:outputLabel value="Previsão" for="previsao" />
						<p:calendar id="previsao" value="#{ordemServicoBean.ordemServico.previsao}" mindate="#{ordemServicoBean.data}" />
						<h:outputText />
						<p:outputLabel value="Observação" for="observacao" />
						<p:inputTextarea id="observacao" value="#{ordemServicoBean.ordemServico.observacao}" rows="5" cols="30" maxlength="255" />
						<h:outputText />
					</p:panelGrid>
					<p:dataTable id="ordemServicoTable" var="item"
								 value="#{ordemServicoBean.itensOrdem}" rows="5" paginator="true"
								 paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
								 currentPageReportTemplate="({currentPage} de {totalPages})" emptyMessage="Nenhum registro encontrado." rendered="#{ordemServicoBean.itensOrdem != null}">
						<p:column headerText="Nome">
							<h:outputText value="#{item.peca.nome}" />
						</p:column>
						<p:column headerText="Quantidade">
							<h:outputText value="#{item.quantidade}" />
						</p:column>
						<p:column headerText="Preço Unitário">
							<h:outputText value="#{item.peca.preco}">
								<f:convertNumber minFractionDigits="2" maxFractionDigits="2" />
							</h:outputText>
						</p:column>
						<p:column headerText="Preço Total">
							<h:outputText value="#{item.peca.preco * item.quantidade}">
								<f:convertNumber type="currency" locale="pt_BR" />
							</h:outputText>
						</p:column>
						<p:column style="width: 32px;">
							<p:commandButton icon="ui-icon-carat-1-n" title="Incrementar" rendered="#{item.quantidade == item.peca.quantidade}" disabled="true" />
							<p:commandButton icon="ui-icon-carat-1-n" title="Incrementar" process="@this" update="ordemServicoTable" actionListener="#{ordemServicoBean.incrementarItemOrdem(item)}" rendered="#{item.peca.quantidade > item.quantidade}" />
						</p:column>
						<p:column style="width: 32px;">
							<p:commandButton icon="ui-icon-carat-1-s" title="Decrementar" rendered="#{item.quantidade == 1}" disabled="true" />
							<p:commandButton icon="ui-icon-carat-1-s" title="Decrementar" process="@this" update="ordemServicoTable" actionListener="#{ordemServicoBean.decrementarItemOrdem(item)}" rendered="#{item.quantidade > 1}" />
						</p:column>
						<p:column style="width: 32px;">
							<p:commandButton icon="ui-icon-trash" title="Remover" process="@this" update="ordemServicoTable" actionListener="#{ordemServicoBean.removerItemOrdem(item)}" />
						</p:column>
					</p:dataTable>
				</p:fieldset>
				<br />
				<p:commandButton id="salvar" value="Salvar" action="#{ordemServicoBean.salvar}" update=":growl, :ordemServicoForm, :datatableForm" icon="ui-icon-disk" />
				<sec:authorize ifAnyGranted="ROLE_ADMIN, ROLE_ENCARREGADO, ROLE_APONTADOR">
					<p:commandButton id="novo" value="Novo" actionListener="#{ordemServicoBean.novo}" process="@this" update=":ordemServicoForm" icon="ui-icon-document" />
				</sec:authorize>
			</h:form>
			<br />
			<h:form id="datatableForm">
				<p:dataTable id="datatable" var="ordemServico" filteredValue="#{ordemServicoBean.filteredOrdemServico}"
					value="#{ordemServicoBean.ordensServico}" rows="5" paginator="true"
					paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
					currentPageReportTemplate="({currentPage} de {totalPages})" rowsPerPageTemplate="10, 15, 20" emptyMessage="Nenhum registro encontrado.">
					<f:facet name="header">
				        <h:outputText value="Lista de Ordens de Serviço" />
				    </f:facet>
					<p:column headerText="Data" filterBy="#{ordemServico.data}" filterMatchMode="contains">
						<h:outputText value="#{ordemServico.data}">
							<f:convertDateTime dateStyle="medium" />
						</h:outputText>
					</p:column>
					<p:column headerText="Veículo Placa" filterBy="#{ordemServico.veiculo.placa}" filterMatchMode="contains">
						<h:outputText value="#{ordemServico.veiculo.placa}" />
					</p:column>
					<p:column headerText="Número">
						<h:outputText value="#{ordemServico.numero}" />
					</p:column>
					<p:column headerText="Status" filterBy="#{ordemServico.statusOS.statusOS}" filterMatchMode="contains">
						<h:outputText value="#{ordemServico.statusOS.statusOS}" />
					</p:column>
					<p:column headerText="Previsão">
						<h:outputText value="#{ordemServico.previsao}">
							<f:convertDateTime dateStyle="medium" />
						</h:outputText>	
					</p:column>
					<sec:authorize ifAnyGranted="ROLE_ADMIN, ROLE_ENCARREGADO, ROLE_CHEFE_MECANICA">
						<p:column style="width: 32px;">
							<p:commandButton icon="ui-icon-pencil" title="Editar" actionListener="#{ordemServicoBean.editar(ordemServico)}" update=":ordemServicoForm" rendered="#{ordemServico.statusOS.statusOS != 'Serviço Concluído'}" />
							<p:commandButton icon="ui-icon-pencil" title="Editar" disabled="true" rendered="#{ordemServico.statusOS.statusOS == 'Serviço Concluído'}" />
						</p:column>
					</sec:authorize>
					<sec:authorize ifAnyGranted="ROLE_ADMIN, ROLE_ENCARREGADO">
						<p:column style="width: 32px;">
							<p:commandButton icon="ui-icon-trash" title="Excluir" actionListener="#{ordemServicoBean.excluir(ordemServico)}" update=":growl, :ordemServicoForm, :datatableForm" rendered="#{ordemServico.statusOS.statusOS != 'Serviço Concluído'}">
								<p:confirm header="Confirmação" message="Deseja realmente excluir esta ordem de serviço" icon="ui-icon-alert" />
							</p:commandButton>
							<p:commandButton icon="ui-icon-trash" title="Excluir" disabled="true" rendered="#{ordemServico.statusOS.statusOS == 'Serviço Concluído'}" />
						</p:column>
					</sec:authorize>
				</p:dataTable>
			</h:form>
		</p:panel>

		<p:dialog id="veiculoDialog" header="Veículos" widgetVar="veiculoDialog" modal="true" resizable="false" width="800px">
			<h:form id="veiculoDialogForm">
				<p:dataTable id="veiculoDialogTable" var="veiculo"
					value="#{ordemServicoBean.veiculos}" rows="5" paginator="true"
					paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
					currentPageReportTemplate="({currentPage} de {totalPages})"
					emptyMessage="Nenhum registro encontrado."
					rendered="#{ordemServicoBean.veiculos != null}">
					<p:column headerText="Placa">
						<h:outputText value="#{veiculo.placa}" />
					</p:column>
					<p:column headerText="Marca">
						<h:outputText value="#{veiculo.modelo.marca.nome}" />
					</p:column>
					<p:column headerText="Modelo">
						<h:outputText value="#{veiculo.modelo.nome}" />
					</p:column>
					<p:column style="width: 32px;">
						<p:commandButton icon="ui-icon-plus" title="Adicionar" update="ordemServicoForm" actionListener="#{ordemServicoBean.adicionarVeiculo(veiculo)}" ajax="false" />
					</p:column>
				</p:dataTable>
			</h:form>
		</p:dialog>
		
		<p:dialog id="pecaDialog" header="Peças" widgetVar="pecaDialog" modal="true" resizable="false" width="800px">
			<h:form id="pecaDialogForm">
				<p:dataTable id="pecaDialogTable" var="peca"
					value="#{ordemServicoBean.pecas}" rows="5" paginator="true"
					paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
					currentPageReportTemplate="({currentPage} de {totalPages})"
					emptyMessage="Nenhum registro encontrado."
					rendered="#{ordemServicoBean.pecas != null}">
					<p:column headerText="Nome">
						<h:outputText value="#{peca.nome}" />
					</p:column>
					<p:column headerText="Quantidade">
						<h:outputText value="#{peca.quantidade}" />
					</p:column>
					<p:column headerText="Preço">
						<h:outputText value="#{peca.preco}">
							<f:convertNumber type="currency" locale="pt_BR" />
						</h:outputText>
					</p:column>
					<p:column style="width: 32px;">
						<p:commandButton icon="ui-icon-plus" title="Adicionar" actionListener="#{ordemServicoBean.adicionarPeca(peca)}" update=":ordemServicoForm:ordemServicoTable" rendered="#{peca.quantidade != 0}" />
						<p:commandButton icon="ui-icon-plus" title="Adicionar" disabled="true" rendered="#{peca.quantidade == 0}" />
					</p:column>
				</p:dataTable>
			</h:form>
		</p:dialog>
	</ui:define>
</ui:composition>

</html>